package org.liuyunfeng.config;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoDriverInformation;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.mongo.MongoClientFactory;
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: TanJingyu
 * @create: 2022-04-01 16:57
 **/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(value = {MongoConfiguration.MongoConProperties.class})
public class MongoConfiguration {

    @Bean
    @ConditionalOnMissingBean(MongoClient.class)
    public MongoClient mongo(MongoProperties properties, Environment environment,
                             ObjectProvider<MongoClientSettingsBuilderCustomizer> builderCustomizers,
                             ObjectProvider<MongoClientSettings> settings) {

        return new MongoClientFactory(properties, environment,
                builderCustomizers.orderedStream().collect(Collectors.toList()))
                .createMongoClient(settings.getIfAvailable());
    }
//
//    @Bean
//    MongoDatabaseFactory mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties) {
//        return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getMongoClientDatabase());
//    }
//
//    @Bean
//    @ConditionalOnMissingBean(MongoOperations.class)
//    MongoTemplate mongoTemplate(MongoDatabaseFactory factory, MongoConverter converter) {
//        return new MongoTemplate(factory, converter);
//    }




    @Bean
    public MongoTemplate mongoTemplate(MongoConProperties mongoConProperties) {
        MongoClientSettings targetSettings = mongoConProperties.computeClientSettings("1");
        MongoClient mongoClient = MongoClients.create(targetSettings,
                MongoDriverInformation.builder(MongoDriverInformation.builder().build()).driverName("spring-boot-01")
                        .build());
        return new MongoTemplate(mongoClient, "zb_content");
    }

    @Bean
    public MongoTemplate mongoTemplate26(MongoConProperties mongoConProperties) {
        MongoClientSettings targetSettings = mongoConProperties.computeClientSettings("26");
        MongoClient mongoClient = MongoClients.create(targetSettings,
                MongoDriverInformation.builder(MongoDriverInformation.builder().build()).driverName("spring-boot-01")
                        .build());
        return new MongoTemplate(mongoClient, "zb_content");
    }

    @Getter
    @Setter
    @ConfigurationProperties(prefix = "mongo")
    public static class MongoConProperties {
        private Integer socketKeepAlive;
        private Integer socketTimeout;
        private Integer connectTimeout;
        private Integer maxWaitTime;
        private Map<String,Object> replicaSet;

        public MongoClientSettings computeClientSettings(String replicaSetKey) {
//            MongoClientSettings.Builder settingsBuilder = (settings != null) ? MongoClientSettings.builder(settings)
//                    : MongoClientSettings.builder();
//            validateConfiguration();
//            applyUuidRepresentation(settingsBuilder);
//            applyHostAndPort(settingsBuilder);
//            applyCredentials(settingsBuilder);
//            applyReplicaSet(settingsBuilder);
//            customize(settingsBuilder);

            return MongoClientSettings.builder().applyConnectionString(new ConnectionString((String) replicaSet.get(replicaSetKey))).build();
        }
    }




}

